<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf;assembly=MaterialDesignThemes.Wpf"
                    xmlns:converters="clr-namespace:MaterialDesignThemes.Wpf.Converters;assembly=MaterialDesignThemes.Wpf"
                    xmlns:dentalConverters="clr-namespace:DentalConfig.Converters;assembly=DentalConfig">

    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="../Brushes/MaterialDesign/MaterialDesignTheme.Theme.xaml"/>
    </ResourceDictionary.MergedDictionaries>

    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    <converters:NullableToVisibilityConverter x:Key="NullableToVisibilityConverter" />
    <dentalConverters:BrushToTransmittantBrushConverter x:Key="BrushToTransmittantBrushConverter"/>
    <converters:MathConverter Operation="Divide" x:Key="DivisionMathConverter" />
    <converters:MathConverter Operation="Subtract" x:Key="SubtractMathConverter" />
    <converters:MathConverter Operation="Add" x:Key="AddMathConverter" />

    <Style TargetType="{x:Type wpf:Chip}">
      <Setter Property="Height" Value="32" />
      <Setter Property="HorizontalAlignment" Value="Left" />
      <Setter Property="FontSize" Value="13" />
      <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
      <Setter Property="Background" Value="{DynamicResource MaterialDesignChipBackground}" />
      <Setter Property="IconBackground" Value="{DynamicResource PrimaryHueMidBrush}" />
      <Setter Property="IconForeground" Value="{DynamicResource PrimaryHueMidForegroundBrush}" />
      <Setter Property="Cursor" Value="Hand"/>
      <Setter Property="Foreground" Value="{DynamicResource MaterialDesignBody}"/>
      <Setter Property="wpf:RippleAssist.Feedback" Value="{DynamicResource MaterialDesignFlatButtonRipple}"/>
      <Setter Property="BorderThickness" Value="0"/>
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type wpf:Chip}">
            <Grid>
              <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="Auto" />
                  <ColumnDefinition Width="*" />
                  <ColumnDefinition Width="Auto" />
                  <ColumnDefinition Width="Auto" />
              </Grid.ColumnDefinitions>
              <Border Background="{TemplateBinding Background}" x:Name="border" CornerRadius="{TemplateBinding Height, Converter={StaticResource DivisionMathConverter}, ConverterParameter=2}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Grid.ColumnSpan="4"/>
              <Grid Grid.ColumnSpan="4">
                <Grid.OpacityMask>
                  <VisualBrush Visual="{Binding ElementName=border}"/>
                </Grid.OpacityMask>
                <wpf:Ripple
                  Focusable="False"
                  RecognizesAccessKey="False"
                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                <Grid x:Name="focusBackground" Background="Transparent" IsHitTestVisible="False"/>
              </Grid>
              <ContentControl Content="{TemplateBinding Icon}"
                              x:Name="IconControl"
                              Background="{TemplateBinding IconBackground}"
                              Foreground="{TemplateBinding IconForeground}"
                              FontSize="17"
                              FontWeight="Regular"
                              IsTabStop="False"
                              IsHitTestVisible="False"
                              Visibility="{TemplateBinding Icon, Converter={StaticResource NullableToVisibilityConverter}}"
                              VerticalAlignment="Center"
                              VerticalContentAlignment="Center"
                              HorizontalContentAlignment="Center"
                              Height="{TemplateBinding Height}" Width="{TemplateBinding Height}">
                  <ContentControl.OpacityMask>
                    <VisualBrush>
                      <VisualBrush.Visual>
                        <Ellipse Fill="{TemplateBinding Background}" Width="{TemplateBinding Height}" Height="{TemplateBinding Height}">
                          <Ellipse.RenderTransform>
                            <TranslateTransform X="{TemplateBinding Height, Converter={StaticResource DivisionMathConverter}, ConverterParameter=2}"
                                                Y="{TemplateBinding Height, Converter={StaticResource DivisionMathConverter}, ConverterParameter=2}" />
                           </Ellipse.RenderTransform>
                        </Ellipse>
                      </VisualBrush.Visual>
                    </VisualBrush>
                  </ContentControl.OpacityMask>
                  <ContentControl.Template>
                      <ControlTemplate TargetType="ContentControl">
                          <Border Background="{TemplateBinding Background}">
                              <ContentPresenter Content="{TemplateBinding Content}"
                                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                          </Border>
                      </ControlTemplate>
                  </ContentControl.Template>
              </ContentControl>
              <ContentControl Content="{TemplateBinding Content}"
                              ContentTemplate="{TemplateBinding ContentTemplate}"
                              ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
                              ContentStringFormat="{TemplateBinding ContentTemplateSelector}"
                              x:Name="TextBlock"
                              IsTabStop="False"
                              IsHitTestVisible="False"
                              VerticalAlignment="Center"
                              Margin="8 0 0 0"
                              Grid.Column="1">
                <ContentControl.Template>
                  <ControlTemplate TargetType="ContentControl">
                    <ContentPresenter Content="{TemplateBinding Content}">
                      <ContentPresenter.Resources>
                        <Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
                          <Setter Property="TextTrimming" Value="CharacterEllipsis"/>
                          <Setter Property="MaxWidth" Value="{Binding MaxWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}, Converter={StaticResource AddMathConverter}, ConverterParameter=1}"/>
                        </Style>
                      </ContentPresenter.Resources>
                    </ContentPresenter>
                  </ControlTemplate>
                </ContentControl.Template>
              </ContentControl>
              <Grid Grid.Column="2" Width="{TemplateBinding Height, Converter={StaticResource DivisionMathConverter}, ConverterParameter=2}"/>
              <Button Grid.Column="3" Visibility="{TemplateBinding IsDeletable, Converter={StaticResource BooleanToVisibilityConverter}}"
                      x:Name="PART_DeleteButton"
                      ToolTip="{TemplateBinding DeleteToolTip}"
                      VerticalAlignment="Center"
                      MinWidth="16" MinHeight="16"
                      MaxWidth="16" MaxHeight="16"
                      Tag="{TemplateBinding Height, Converter={StaticResource SubtractMathConverter}, ConverterParameter=16}">
                  <Button.RenderTransform>
                    <TranslateTransform X="{Binding Tag, ElementName=PART_DeleteButton, Converter={StaticResource DivisionMathConverter}, ConverterParameter=-2}" Y="0"/>
                  </Button.RenderTransform>
                  <Button.Template>
                      <ControlTemplate TargetType="{x:Type Button}">
                          <Grid>
                            <Ellipse x:Name="Bg" Fill="{DynamicResource MaterialDesignChipDeleteBackground}"
                              Stroke="{Binding Foreground, ElementName=CloseIcon, Converter={StaticResource BrushToTransmittantBrushConverter}, ConverterParameter=0.5}"
                              StrokeThickness="0"/>
                            <Grid>
                              <Grid.OpacityMask>
                                <VisualBrush>
                                  <VisualBrush.Visual>
                                    <Ellipse Fill="{DynamicResource MaterialDesignChipDeleteBackground}" Width="16" Height="16"/>
                                  </VisualBrush.Visual>
                                </VisualBrush>
                              </Grid.OpacityMask>
                              <wpf:Ripple
                                Focusable="False"
                                RecognizesAccessKey="False"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                Feedback="{DynamicResource MaterialDesignChipDeleteForeground}"/>
                              <Grid x:Name="deleteFocusBackground" Background="Transparent" IsHitTestVisible="False"/>
                            </Grid>
                            <wpf:PackIcon x:Name="CloseIcon" Kind="Close" Width="12" Height="12"
                                          HorizontalAlignment="Center"
                                          VerticalAlignment="Center"
                                          Foreground="{DynamicResource MaterialDesignChipDeleteForeground}"
                                          IsHitTestVisible="False"/>
                          </Grid>
                          <ControlTemplate.Triggers>
                            <Trigger Property="IsFocused" Value="True">
                              <Setter Property="Background" TargetName="deleteFocusBackground" Value="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BrushToTransmittantBrushConverter}, ConverterParameter=0.16}"/>
                              <Setter TargetName="Bg" Property="StrokeThickness" Value="1" />
                              <Setter TargetName="Bg" Property="Margin" Value="-1" />
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                              <Setter Property="Background" TargetName="deleteFocusBackground" Value="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BrushToTransmittantBrushConverter}, ConverterParameter=0.32}"/>
                            </Trigger>
                          </ControlTemplate.Triggers>
                      </ControlTemplate>
                  </Button.Template>
              </Button>
            </Grid>
            <ControlTemplate.Triggers>
              <Trigger Property="IsFocused" Value="True">
                <Setter Property="Background" TargetName="focusBackground" Value="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BrushToTransmittantBrushConverter}, ConverterParameter=0.04}"/>
              </Trigger>
              <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="Background" TargetName="focusBackground" Value="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BrushToTransmittantBrushConverter}, ConverterParameter=0.08}"/>
              </Trigger>
              <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Opacity" Value="{StaticResource DisabledOpacity}"/>
              </Trigger>
            </ControlTemplate.Triggers>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
</ResourceDictionary>
